﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Bzure.Apps.Security.ViewModels;
using Bzure.Common.Patterns;
using System.Threading;
using System.Web;
using System.Web.Routing;
using Bzure.Apps.Security;

namespace System
{
    public static class UrlExtensions
    {
        public static UrlHelper CurrentUrlHelper(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            return new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
        }
    }
    public static class AccountUrlExtensions
    {
        #region LogonUrl
        public static string LoginUrl(this Public Public)
        {
            string returnurl = HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.PathAndQuery);
            return string.Format("{0}?returnUrl={1}", Actor.Public.UserLogOnUrl(), returnurl);
        }
        public static string UserLogOnUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_Login", new { controller = "Account", action = "logon" });
        }
        public static string UserLogOnUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.UserLogOnUrl();
        }
        #endregion LogonUrl
        #region LogoffUrl
        public static string UserLogOffUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_Login", new { controller = "Account", action = "logoff" });
        }
        public static string UserLogOffUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.UserLogOffUrl();
        }
        #endregion logoffUrl
        #region UserRegisterUrl
        public static string UserRegisterUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_Login", new { controller = "Account", action = "Register" });
        }
        public static string UserRegisterUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.UserRegisterUrl();
        }
        #endregion UserRegisterUrl
        #region UserChangePasswordUrl
        public static string UserChangePasswordUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_Login", new { controller = "Account", action = "ChangePassword" });
        }
        public static string UserChangePasswordUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.UserChangePasswordUrl();
        }
        #endregion UserChangePasswordUrl
        #region ResetNickNameUrl
        public static string UserResetNickNameUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_User", new { controller = "User", action = "ResetNickName" });
        }
        public static string UserResetNickNameUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.UserResetNickNameUrl();
        }
        #endregion
    }
    public static class UserUrlExtensions
    {
        #region UserListUrl
        public static string UserListUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_User", new { controller = "User", action = "UserList" });
        }
        public static string UserListUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.UserListUrl();
        }
        #endregion UserListUrl
    }
    public static class RoleUrlExtensions
    {
        #region RoleListUrl
        public static string RoleListUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_Role", new { controller = "Role", action = "RoleList" });
        }
        public static string RoleListUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.RoleListUrl();
        }
        #endregion RoleListUrl
        #region RoleNewUrl
        public static string RoleNewUrl(this UrlHelper Url)
        {
            return Url.RouteUrl("SecurityApp_Role", new { controller = "Role", action = "NewRole" });
        }
        public static string RoleNewUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.RoleNewUrl();
        }
        #endregion RoleNewUrl
    }
    public static class DroitUrlExtensions
    {
        #region DroitsConfigUrl
        public static IEnumerable<KeyValuePair<string, Dictionary<string, string>>> DroitsConfigUrl(this Public Public)
        {
            HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
            UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
            return Url.DroitsConfigUrl();
        }
        public static IEnumerable<KeyValuePair<string,Dictionary<string,string>>> DroitsConfigUrl(this UrlHelper Url)
        {
            foreach (KeyValuePair <string,Dictionary<Type,string>> paire in Actor.Public.GroupedSystemRightTypes ())
           {
               Dictionary<string,string> dic=new Dictionary<string,string> (); 
               foreach (KeyValuePair<Type, string> resource in paire.Value )
               {
                   string url = Url.RouteUrl("SecurityApp_Droit", new { controller = "Droit", action = "Config", id = resource.Key.FullName });
                   dic[url] = resource.Value;
               }
               //returnDic[paire.Key] = dic; 
               yield return new KeyValuePair<string, Dictionary<string, string>>(paire.Key, dic);
           }
           //return returnDic;
        }

        #endregion DroitsConfigUrl
    }
    public static class MenuExtensions
    {
        delegate string AsyncEventHandler(UrlHelper Url);
        //public static string ManageMenuBody(this Public Public)
        //{
        //    if (!Actor.Public.IsAuthenticated)
        //        return string.Empty;
        //    HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
        //    UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
        //    return Url.ManageMenuBody();
        //}
        //public static string ManageMenuBody(this UrlHelper Url)
        //{
        //    return Singleton<Bzure.Apps.Security.ResourceFiles.ResourceAccess>.Instance.GetCached(" _SecurityApp_ManagedMenuBody", () =>
        //    {
        //        //AsyncEventHandler async = MenuExtensions.body;
        //        //IAsyncResult result = async.BeginInvoke(Url, null, null);
        //        ////这里会阻碍线程,直到方法执行完毕
        //        //string val = async.EndInvoke(result);
        //        //return val;
        //        return body(Url);
        //    }, TimeSpan.FromMinutes(60));
        //}
        //static string body(UrlHelper Url)
        //{
        //    T4Engine engine = new T4Engine(Url);
          
        //    engine.AddTag(new T4Tag("User_MENU", Url.UserListUrl()));
        //    engine.AddTag(new T4Tag("ROLE_NEW_MENU", Url.RoleNewUrl()));
        //    engine.AddTag(new T4Tag("ROLE_LIST_MENU", Url.RoleListUrl()));
        //    List<T4Tag> list = new List<T4Tag>();
        //    foreach (KeyValuePair<string, string> pair in Url.DroitsConfigUrl())
        //    {
        //        list.Add(new T4Tag(pair.Value, pair.Key));
        //    }
        //    engine.AddParameter("DROIT_MENU", list.ToArray());
        //    string content = engine.Execute(new ManageMenuViewModel());
        //    return content;
        //}
        
        //public static IEnumerable<KeyValuePair<string, string>> SecurityAppMenus(this UrlHelper Url)
        //{
        //    Dictionary<string, string> dic = new Dictionary<string, string>();
        //    dic[TagHelper.ChangePasswordUrl] = Url.UserChangePasswordUrl();
        //    dic[TagHelper.LogoffUrl] = Url.UserLogOffUrl();
        //    dic[TagHelper.LogonUrl] = Url.UserLogOnUrl();
        //    dic[TagHelper.NewRole] = Url.RoleNewUrl();
        //    dic[TagHelper.RegisterUrl] = Url.UserRegisterUrl();
        //    dic[TagHelper.RoleList] = Url.RoleListUrl();
        //    dic[TagHelper.UserList] = Url.UserListUrl();
        //    return dic;
        //}
        //public static IEnumerable<KeyValuePair<string, string>> SecurityAppMenus(this Public Public)
        //{
        //    HttpContextWrapper hcw = new HttpContextWrapper(HttpContext.Current);
        //    UrlHelper Url = new UrlHelper(new RequestContext(hcw, RouteTable.Routes.GetRouteData(hcw)));
        //    return Url.SecurityAppMenus();
        //}
    }
}
